﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Dynamo.DesktopModules.Admin
{


    public partial class DynamoAdminSettings : DotNetNuke.Entities.Modules.PortalModuleBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    List<Type> t = AdminPermissions.IPermissionTypeList;
                    SecurityOptionsCheckboxList.DataSource = t;
                    SecurityOptionsCheckboxList.DataBind();

                    string items = Dynamo.AdminPermissions.EnabledPermissions;
                    if (items != null && items.Trim() != "")
                    {
                        foreach (System.Web.UI.WebControls.ListItem item in SecurityOptionsCheckboxList.Items)
                        {
                            item.Selected = false;
                            if (items.Contains(item.Text + ";")) item.Selected = true;
                        }
                    }


                    EnableScripting.Checked = Dynamo.AdminPermissions.ScriptingEnabled;
                    this.ScriptingEnabledPanel.Enabled = EnableScripting.Checked;
                    IgnoreSecurityCheckbox.Checked = Dynamo.AdminPermissions.IgnoreSecurity;
                    SecurityOptionsPanel.Enabled = !IgnoreSecurityCheckbox.Checked;
                    AllowCLRCheckbox.Checked = AdminPermissions.AllowCLR;
                }
                catch (Exception)
                {
                    SecurityOptionsCheckboxList.Enabled = false;
                }
            }
        }

        protected void EnableScripting_CheckedChanged(object sender, EventArgs e)
        {
            this.ScriptingEnabledPanel.Enabled = EnableScripting.Checked;
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {

            Dynamo.AdminPermissions.ScriptingEnabled = this.EnableScripting.Checked;

            if (EnableScripting.Checked)
            {
                AdminPermissions.AllowCLR = AllowCLRCheckbox.Checked;

                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                foreach (System.Web.UI.WebControls.ListItem item in SecurityOptionsCheckboxList.Items)
                {
                    if (item.Selected) {
                        sb.Append(item.Text);
                        sb.Append(";");
                    }
                }
                Dynamo.AdminPermissions.EnabledPermissions = sb.ToString();
            }
            Dynamo.AdminPermissions.IgnoreSecurity = IgnoreSecurityCheckbox.Checked;
            
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
        }

        protected void IgnoreSecurityCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            AdminPermissions.IgnoreSecurity = IgnoreSecurityCheckbox.Checked;
            SecurityOptionsPanel.Enabled = !IgnoreSecurityCheckbox.Checked;
        }
    }
}